﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Chowpoo.Common;
using Chowpoo.Util;
using CourseSystem.Common.Helpers;
using CourseSystem.Core.BLL;
using CourseSystem.Core.Entities;
using CourseSystem.Core.Enum;
using Webdiyer.WebControls.Mvc;

namespace CourseSystem.Web.Controllers
{
    public class ManageController : Controller
    {
        //
        // GET: /Manage/

        public ActionResult UserList()
        {
            return View();
        }

        public ActionResult UserListUc(int? pid)
        {
            CommonRequest.ClearCache();
            var list = TeacherBLL.GetList().OrderByDescending(o => o.Id);
            var model = list.ToPagedList(pid ?? 1, CommonHelper.PageSize);
            return PartialView(model);
        }

        public ActionResult UserEditUc(int? id)
        {
            if (id.HasValue)
            {
                var info = TeacherBLL.Get(id.Value);
                if (info == null)
                    return View("NotFound");
                return PartialView(info);
            }
            return PartialView(new TeacherInfo { Account = "", Name = "" });
        }

        [HttpPost]
        [UserAuth(UserType=UserTypeEnum.Admin)]
        public ActionResult UserEditUc(TeacherInfo info)
        {
            ViewData.Model = info;
            if (!ModelState.IsValid)
                return PartialView();


            if (info.Id != 0)
            {
                var oldInfo = TeacherBLL.Get(info.Id);
                if (oldInfo == null)
                    return View("NotFound");

                oldInfo.Account = info.Account;
                oldInfo.Name = info.Name;
                oldInfo.IsAdmin = info.IsAdmin;
                //强制修改密码
                if (!string.IsNullOrEmpty(info.Pass))
                    oldInfo.Pass = Security.Pass2Md5(info.Pass);

                if (TeacherBLL.IsExist(oldInfo.Account, oldInfo.Id))
                {
                    ModelState.AddModelError("Account", "已存在相同账号.");
                    return PartialView();
                }
                if (TeacherBLL.Edit(oldInfo))
                {
                    ViewData["Msg"] = "编辑成功.";
                    return PartialView(oldInfo);
                }
                ViewData["Msg"] = "未知错误.";
                return PartialView();
            }

            if (TeacherBLL.IsExist(info.Account))
            {
                ModelState.AddModelError("Account", "已存在相同账号.");
                return PartialView();
            }
            if(string.IsNullOrEmpty(info.Pass))
            {
                ModelState.AddModelError("Pass", "必须填写");
                return PartialView();
            }
            if (TeacherBLL.Add(info))
            {
                ViewData["Msg"] = "编辑成功.";
                return PartialView(info);
            }
            ViewData["Msg"] = "未知错误.";
            return PartialView();
        }

        public ActionResult UserDel(int id)
        {
            if (TeacherBLL.Del(id))
            {
                TempData["AlertMsg"] = "删除成功.";
            }
            else
                TempData["AlertMsg"] = "未知错误.";
            return RedirectToAction("UserList");

        }
    }
}
